$(function () {
    init_data();
    var cusplanId = sessionStorage.getItem("planCusBtn");
    if(cusplanId==='1'){
        $("#sendPlanId").attr("disabled","disabled");
    }else {
        $("#sendPlanId").removeAttr("disabled");
    }
});

function init_data() {
    var id = sessionStorage.getItem("planId");
    $.post(ctx+"SaleHelp/getPlanCusList",{cPlanId:id},function (r) {
        if(r.code===0){
            var data = r.data;
            var svg = '';
            if(data.cPlanType==='1'){
                svg = '<svg class="bi bi-alarm-fill p-t-4" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">\n' +
                    '                            <path fill-rule="evenodd" d="M5.5.5A.5.5 0 0 1 6 0h4a.5.5 0 0 1 0 1H9v1.07a7.002 7.002 0 0 1 3.537 12.26l.817.816a.5.5 0 0 1-.708.708l-.924-.925A6.967 6.967 0 0 1 8 16a6.967 6.967 0 0 1-3.722-1.07l-.924.924a.5.5 0 0 1-.708-.708l.817-.816A7.002 7.002 0 0 1 7 2.07V1H5.999a.5.5 0 0 1-.5-.5zM.86 5.387A2.5 2.5 0 1 1 4.387 1.86 8.035 8.035 0 0 0 .86 5.387zM13.5 1c-.753 0-1.429.333-1.887.86a8.035 8.035 0 0 1 3.527 3.527A2.5 2.5 0 0 0 13.5 1zm-5 4a.5.5 0 0 0-1 0v3.882l-1.447 2.894a.5.5 0 1 0 .894.448l1.5-3A.5.5 0 0 0 8.5 9V5z"/>\n' +
                    '                        </svg>';
            }else{
                svg = '<i class=\"fa fa-phone text-success\"></i>';
            }
            $("#titleId").empty().append(svg+data.cPlanTitle);
            var time = data.cPlanTime.substring(0,16);
            $("#tixingTime").empty().append(time);
            $("#contentId").empty().append(data.cPlanDesc);
            inti_customer(data.cPlanUuid);
        }
    });
}

function inti_customer(uuid) {
    var settings = {
        url: ctx + "saleCustomer/getPlanCus",
        pageSize: 10,
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                cpPid:uuid
            };
        },
        columns: [ {
            field: 'cpCusName',
            title: '客户',
            class:'text-center'
        }, {
            field: 'cpPhone',
            title: '电话',
            class:'text-center'
        },{
            field: 'cpStatus',
            title: '执行详情',
            class:'text-center',
            formatter:function (value, row, index) {
                var pid = row.cpPid;
                $("#sendPlanId").attr("puid",pid);
                if(value==="1"){
                    return "未执行";
                }else if(value==="2"){
                    return "已执行";
                }
            }
        },{
            field: 'cpTime',
            title: '执行时间',
            class:'text-center'
        },{
            field: 'cpStatus',
            title: '操作',
            class:'text-center',
            formatter:function (value, row, index) {
                var id = sessionStorage.getItem("PlanCusId");
                if(value==="1"){
                    return "<button class='btn btn-sm btn-link p-0' uuid='"+id+"' onclick='seelctPlanDetail(this)' >编辑</button>";
                }else if(value==="2"){
                    return "<button class='btn btn-sm btn-link p-0' disabled>编辑</button>";
                }
            }
        }]

    };
    $JuXue.initTable('salePlanCusTable', settings);
}

function nowSend(btn) {
    var id =  $(btn).attr("puid");
    $.getJSON(ctx+"saleCustomer/newSendPlan",{cpPid:id,cpType:'1'},function (r) {
        if(r.code===0){
            $("#sendPlanId").attr("disabled","disabled");
            $JuXue.refreshTable("salePlanCusTable");
            $JuXue.n_success("操作成功");
        }else{
            $JuXue.n_danger("操作失败");
        }
    })
}